package com.lvcoding.spring5.annotation3x.zhenghe;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

/**
 * @Description 描述
 * @Date 2020-09-08 3:08 下午
 * @Author wuyanshen
 */
@MapperScan(basePackages = "com.lvcoding.spring5.annotation3x.zhenghe")
@Configuration
public class AppConfig {

    @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/mbtest?useSSL=false&characterEncoding=utf8");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        return druidDataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.lvcoding.spring5.annotation3x.zhenghe");
        sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("mapper/*.xml"));
        return sqlSessionFactoryBean;
    }
}
